package test.baojia.act.operation;

import com.alibaba.fastjson.JSONObject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.Assert;

import java.util.Properties;

/**
 * Checkbox框控件操作。
 */
public class CheckboxOperation extends WriteOperation {

    private final static String MESSAGE_TEMPLATE = "#%s can not be operated.";

    public void run(JSONObject context, WebDriver driver, Properties resource, JSONObject parentContext)throws  Exception {
        super.run(context , driver , resource, parentContext);
        final boolean bySelect = context.getBoolean(OPERATION.bySelect.toString());
        WebElement we = this.makeWebElement(true);
        if(bySelect != we.isSelected()){
            we.click();
        }

        final boolean byCheck = (boolean) this.context.get(OPERATION.byCheck.toString());

        if(byCheck){
            Assert.assertTrue(
                    bySelect == we.isSelected() ,
                    String.format(MESSAGE_TEMPLATE , this.context.get(OPERATION.byExp.toString())));
        }
    }

}
